<template>
	<view class="container">
		<u-form :model="form" ref="uForm" label-position="top" :error-type="['toast']">
			<u-form-item label="时间" prop="date">
				<u-input v-model="form.date" type="select" placeholder="请选择时间" @click="timeShow1 = true" />
			</u-form-item>
			<u-form-item label="叮嘱" prop="content">
				<u-input v-model="form.content" type="textarea"/>
			</u-form-item>
		</u-form>
		<u-button class="action-view" @click="saveClick">保存</u-button>
		<u-picker v-model="timeShow1" mode="time" :params="params" @confirm="confirmTime1"></u-picker>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	import {
		addRecordFeed
	} from '@/api/record.js'
	import {
		formatDateByTimestamp
	} from '@/utils/utils.js'
	import recordMixin from './mixins/index.js'
	export default {
		mixins: [recordMixin],
		data() {
			return {
				form: {
					date: formatDateByTimestamp(Date.now(), 'yyyy/mm/dd hh:MM:ss'),
					content: '',
				},
				rules: {
					date: [{
						required: true,
						message: '请选择叮咛时间',
					}, ],
					content: [{
						required: true,
						message: '请填写叮嘱内容',
					}, ],
				},

				timeShow1: false,
				params: {
					year: false,
					month: false,
					day: false,
					hour: true,
					minute: true,
					second: false,
					timestamp: true
				},
			}
		},
		computed: {
			...mapState({
				appType: state => state.app.appType,
				parentInfo: state => state.user.parentInfo,
			}),
		},
		onLoad() {
			this.childrenList.push(this.parentInfo.defaultBaby)
		},
		methods: {
			addRecord() {
				let childIds = '';
				for (let index in this.childrenList) {
					let item = this.childrenList[index];
					childIds += `,${item.id}`;
				}
				if (childIds.length > 0) {
					childIds = childIds.substring(1);
				}
				if (!this.$u.test.isEmpty(this.recordId)) {
					this.form.id = this.recordId
					this.form.childrenId = childIds
				}else {
					this.form.childrenId = childIds
				}
				addRecordFeed(this.form).then(res => {
					this.$u.toast('添加成功');
					setTimeout(function(){
						uni.navigateBack({delta:1})
					},1000)
				})
			},

			confirmTime1(res) {
				console.log(res);
				this.form.sleepTime = formatDateByTimestamp(res.timestamp, 'yyyy/mm/dd hh:MM:ss');
			},

			getRecord() {
				console.log(this.recordId)
				if (this.$u.test.isEmpty(this.recordId)) {
					return;
				}
				uni.showLoading({})
				const params = {
					id: this.recordId,
					childId: this.defaultChildren.id
				}
				getRecordSleep(params).then(res => {
					uni.hideLoading()
					const { rankVal, remark } = res.data; 
					const eventTime = formatDateByTimestamp(parseFloat(res.data.eventTime), 'yyyy/mm/dd hh:MM:ss')
					const sleepTime = formatDateByTimestamp(parseFloat(res.data.sleepTime), 'yyyy/mm/dd hh:MM:ss')
					const wakeupTime = formatDateByTimestamp(parseFloat(res.data.wakeupTime), 'yyyy/mm/dd hh:MM:ss')
					const form = {
						eventTime,
						sleepTime,
						wakeupTime,
						rankVal,
						remark,
					};
					this.form = form
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: $app-page-bg-color;
	}
</style>
<style lang="scss" scoped>
	@import '@/css/u-form.scss';
	
	.container {
		padding-left: 30rpx;
		padding-right: 30rpx;
		display: flex;
		flex-direction: column;
	}

	.children-view {
		width: 10rpx;
		flex: 1;
		display: flex;
		flex-direction: row;
	}
</style>
